package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.os.Parcelable;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.diyview.SelectCustomerDialog;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.model.response.VisitTypeResponse;
import com.topboya.linker.model.save.VisitCustomerBean;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CreateTaskActivity extends BaseActivity {
    private static final String TAG = "CreateTaskActivity:";
    private TextView toolbar_title;
    private TextView tv_device_name;
    private LinearLayout ll_create_task;
    private LinearLayout ll_add_customer;
    private OrganizationResopnse.DocDetailInfoList docDetailInfo;
    private ArrayList<String> selectepartnameList;
    private  LinearLayout.LayoutParams layoutParams;
    private List<CustomerByOrgIdResponse.DepartmentList> departmentList;
    private List<String> alldepartmentList;
    private List<String> tempdepartmentList;
    private Button btn_save;
    private List<VisitCustomerBean> visitCustomerBeans ;
//    返回选择的学术类型
    private ArrayList<VisitTypeResponse.Data> visittypelist;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_create_task;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        tv_device_name = findViewById(R.id.tv_device_name);
        ll_create_task = findViewById(R.id.ll_create_task);
        ll_add_customer = findViewById(R.id.ll_add_customer);
        btn_save = findViewById(R.id.btn_save);
        toolbar_title.setText("创建任务");
    }

    @Override
    protected void initData() {

        initdocDetailInfo();
//      每次进来初始化重置view下标
        viewindex=0;
        departmentList = (List<CustomerByOrgIdResponse.DepartmentList>)getIntent().getSerializableExtra("departmentList");
        selectepartnameList = getIntent().getStringArrayListExtra("selectepartnameList");

//      保存部门下下面所有的客户信息
        alldepartmentList = new ArrayList<>();
        tempdepartmentList = new ArrayList<>();
        visitCustomerBeans = new ArrayList<>();
        for (int i=0;i <departmentList.size();i++){

            alldepartmentList.add(departmentList.get(i).getCusName());
        }
//        存放剩余的
        tempdepartmentList.clear();
        tempdepartmentList.addAll(alldepartmentList);
//      根据客户信息初始化几个view
//        initCustomerView();

    }

    private void initdocDetailInfo() {

        docDetailInfo = (OrganizationResopnse.DocDetailInfoList) getIntent().getSerializableExtra("DocDetailInfo");
        String devicesname = "<font color=\"#AAAAAA\">"+"终端名称："+"</font>"+"<font color=\"#000000\">"+docDetailInfo.getOrgName()+"</font>";
        tv_device_name.setText(Html.fromHtml(devicesname));

    }

    int viewindex =0;
    private void initCustomerView() {

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 50, 0, 0);
        for(int i =0; i <selectepartnameList.size();i++){

            addView(layoutParams, viewindex,true);
        }
    }

    private void addView(LinearLayout.LayoutParams layoutParams, int i,boolean isfirst) {

        View view = View.inflate(this,R.layout.create_task_item,null);
        view.setLayoutParams(layoutParams);
        VisitCustomerBean visitCustomerBean =new VisitCustomerBean();
        TextView tv_sustomer_name = view.findViewById(R.id.tv_sustomer_name);
        TextView tv_delete = view.findViewById(R.id.tv_delete);
        View viewline = view.findViewById(R.id.view_line);
        if (0==viewindex) {

            tv_delete.setVisibility(View.GONE);
            viewline.setVisibility(View.GONE);
        }

        if (isfirst){
            //第一次进来就直接添加选中的客户
//            tv_sustomer_name.setText(selectepartnameList.get(i));
//            visitCustomerBean.setName(selectepartnameList.get(i));
            visitCustomerBeans.add(visitCustomerBean);
            }else {

            tv_sustomer_name.setText("请选择客户姓名");
        }

        tv_delete.setOnClickListener(v1 -> {

            ll_create_task.removeView(view);
            visitCustomerBeans.remove(viewindex);
            viewindex--;

        });

        tv_sustomer_name.setOnClickListener(v -> showSelectDialog((ArrayList<String>) tempdepartmentList));


        ll_create_task.addView(view);
        viewindex++;
    }

    private void showSelectDialog(ArrayList<String> tempdepartmentList) {

        final SelectCustomerDialog selectCustomerDialog = SelectCustomerDialog.newInstance(tempdepartmentList);
        selectCustomerDialog.show(getFragmentManager(), "select");
        selectCustomerDialog.setCancelable(false);
        selectCustomerDialog.setOnSelectListener(selectPosSet -> {

            Iterator<Integer> value = selectPosSet.iterator();
            while (value.hasNext()) {

                int s = value.next();
                LogUtils.v(TAG + "s:"+s);

            }
            selectCustomerDialog.dismiss();

        });

    }





    @Override
    protected void initListener() {

        ll_add_customer.setOnClickListener(v -> {

             if (ll_create_task.getChildCount() < alldepartmentList.size()){

                 addView(layoutParams,viewindex,false);

             }else {

                 UIUtils.showToast("没有可添加的用户");
             }
        });
        btn_save.setOnClickListener(v -> {

                Intent intent = new Intent(CreateTaskActivity.this,SellectProductInforActivity.class);
                startActivityForResult(intent,1001);


        });
    }

    @Override
    protected BasePresenter createPresenter() {

        return null;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    //返回选择的学术类型
//        visittypelist = (ArrayList<VisitTypeResponse.Data>) data.getSerializableExtra("result");
//        LogUtils.v(TAG + "result:"+visittypelist.toString());


    }
}
